package models

import (
	"fly/schema"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// AircraftComponent 飞机组件
type AircraftComponent struct {
	BaseModel
	Code              string     `orm:"column(code);description(组件编号)" json:"code"`
	Name              string     `orm:"column(name);description(组件名字)" json:"name"`
	Type              int        `orm:"column(type);description(配件类型)" json:"type"`
	AircraftId        int64      `orm:"column(aircraft_id);description(飞机Id)" json:"aircraftId"`
	WorkCount         int        `orm:"column(work_count);description(工作次数)" json:"workCount"`
	IntervalCount     int        `orm:"column(interval_count);description(下次保养次数间隔)" json:"intervalCount"`
	WorkDuration      int        `orm:"column(work_duration);description(工作时长，单位秒)" json:"workDuration"`
	IntervalDuration  int        `orm:"column(interval_duration);description(下次保养时间间隔，单位秒)" json:"intervalDuration"`
	StageWorkCount    int        `orm:"column(stage_work_count);description(阶段启动次数)" json:"stageWorkCount"`
	PushbackDoorCount int        `orm:"column(pushback_door_count);description(总反推门打开次数)" json:"pushbackDoorCount"`
	RepairCount       int        `orm:"column(repair_count);description(大修次数)" json:"repairCount"`
	DeptId            int64      `orm:"column(dept_id);description(所属部门)" json:"deptId"`
	StateDictId       int64      `orm:"column(state_dict_id);description(发动机状态对应字典Id)" json:"stateDictId"`
	FactoryTime       *time.Time `orm:"column(factory_time);description(出厂时间)" json:"factoryTime"`
	StartUseTime      *time.Time `orm:"column(start_use_time);description(开始使用时间)" json:"startUseTime"`
	AcceptanceTime    *time.Time `orm:"column(acceptance_time);description(验收时间)" json:"acceptanceTime"`
	TakeoffDuration   int        `orm:"column(takeoff_duration);description(起飞状态工作时间:秒)" json:"takeoffDuration"`
	RatedDuration     int        `orm:"column(rated_duration);description(额定状态工作时间:秒)" json:"ratedDuration"`
	LatestFlightTime  *time.Time `orm:"column(latest_flight_time);description(最新飞行时间时间)" json:"latestFlightTime"`
}

func init() {
	orm.RegisterModel(new(AircraftComponent))
}

// SimpleField 简略查询字段
func (*AircraftComponent) SimpleField() []string {
	return []string{"id", "name", "code", "type", "dept_id", "aircraft_id", "work_count", "work_duration"}
}

// SearchField 定义模型的可搜索字段
func (*AircraftComponent) SearchField() []string {
	return []string{"name"}
}

// WhereField 定义模型可作为条件的字段
func (*AircraftComponent) WhereField() []string {
	return []string{"id", "status", "type"}
}

func (m *AircraftComponent) TableName() string {
	return "aircraft_component"
}

// AddAircraftComponent insert a new AircraftComponent into database and returns
// last inserted Id on success.
func AddAircraftComponent(m *AircraftComponent, userId int64) (id int64, err error) {
	o := orm.NewOrm()
	m.Creator = userId
	m.Updater = userId
	id, err = o.Insert(m)
	return
}

// GetAircraftComponentById retrieves AircraftComponent by Id. Returns error if
// Id doesn't exist
func GetAircraftComponentById(id int64) (v *AircraftComponent, err error) {
	o := orm.NewOrm()
	v = &AircraftComponent{BaseModel: BaseModel{Id: id}}
	if err = o.QueryTable(new(AircraftComponent)).Filter("id", id).RelatedSel().One(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateAircraftComponentById updates AircraftComponent by Id and returns error if
// the record to be updated doesn't exist
func UpdateAircraftComponentById(m *AircraftComponent, userId int64) (err error) {
	o := orm.NewOrm()
	v := AircraftComponent{BaseModel: BaseModel{Id: m.Id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		m.Updater = userId
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteAircraftComponent deletes AircraftComponent by Id and returns error if
// the record to be deleted doesn't exist
func DeleteAircraftComponent(id int64) (err error) {
	o := orm.NewOrm()
	v := AircraftComponent{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&AircraftComponent{BaseModel: BaseModel{Id: id}}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

// DestroyAircraftComponent Destroy AircraftComponent by Id and returns error if
func DestroyAircraftComponent(id int64, userId int64) (err error) {
	o := orm.NewOrm()
	v := AircraftComponent{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		v.Status = schema.StatusDelete
		v.Updater = userId
		var num int64
		if num, err = o.Update(v, "updater", "status"); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}
